'''
Author       : ChangZhY
Date         : 2024-04-09 23:48
LastEditors  : ChangZhY
LastEditTime : 2024-04-09 23:54
Desc         : 
'''
# encoding=utf8

import xlrd
import json

path = "C:\\Users\\issac\\HUNGRY_BLOCK\\tools\\"

file = xlrd.open_workbook(path + "config.xls")

config = {
    "level": [],
    "toys": []
}
    

level_sheet = file.sheet_by_name("level_1")
for i in range(level_sheet.nrows):
    for j in range(13):
        value = level_sheet.cell_value(i, j)
        if value:
            block = int(value)
        else:
            block = 0
        level = config["level"]
        if len(level) <= i:
            level.append([])
        level[i].append(block)

toys_sheet = file.sheet_by_name("toys")
keys = ["type", "x", "color"]
for i in range(toys_sheet.nrows):
    if not i:
        continue
    toy = config["toys"]
    toy.append({})
    for j in range(3):
        value = int(toys_sheet.cell_value(i, j))
        toy[i - 1][keys[j]] = value


# print(config)
with open(path + "config.json", "+a") as f:
    f.truncate(0)
    json.dump(config, f)